use futures::StreamExt;
use chromiumoxide::{Browser, BrowserConfig};

#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let (browser, mut handler) =
        Browser::launch(BrowserConfig::builder().with_head().build()?).await?;

    let handle = async_std::task::spawn(async move {
        loop {
            let _event = handler.next().await.unwrap();
        }
    });
    /*
	// open the website
    let page = browser.new_page("https://www.damai.cn/").await?;

    // type into the search field and hit `Enter`,
    // this triggers a navigation to the search result page
    page.find_element(".input-search").await?.click().await?.type_str("dengziqi").await?.press_key("Enter").await?;

  //  let _html = page.wait_for_navigation().await?.content().await?;
    println!("the list page");
    
 */
 
	let page = browser.new_page("https://www.oschina.net/").await?;
	let elements = page.find_elements(".panel-content").await?;
	println!("first:   {}", elements.len());
	let second_eles = elements[0].find_elements(".headline").await?;
	println!("second:   {}", second_eles.len());
	let _third_eles = second_eles[0].click().await?;

	
	let  current_page = page.wait_for_navigation().await?;
	println!("new page");
	let logo = current_page.find_element(".logo").await?;
	logo.click().await?;
	
	
	
	let pages = browser.pages().await?;
	println!("open {} pages", pages.len());
	let target_page = &pages[0];
	println!("open {:?} pages", target_page.target_id());
	println!("{}", target_page.content().await?);
	
	//target_page.find_element(".btn-publish").await?.click().await?;
 
	handle.await;
    
    Ok(())
}
